﻿using System;
using UnityEngine;
using UnityEngine.UI;
using BridgeShips.GUISystem;

namespace BridgeShips.Control {
    public class BuildingShipList : GUIBehaviour {

        [SerializeField]
        private Window m_Window = null;

        [SerializeField]
        private Camera m_GUICamera;

        [Header( "Audio" )]
        [SerializeField]
        private SoundPlayer m_RefreshAudio;

        [SerializeField]
        private SoundPlayer m_SelectButtonAudio;

        private BuildingCategory[] m_Categories;


        private float m_CategoryScrollPos;
        private Button btn;

        private void Update() {
            if (!m_Window.IsOpen)
                return;

            float scrollValue = Player.ScrollValue.Get();

        }

        private void Start() {

            Player.Objects.AddStartTryer( TryStart_ShipList );
            Player.Objects.AddStopTryer( TryStop_ShipList );

            InventoryController.Instance.State.AddChangeListener( OnChanged_InventoryState );
            btn = transform.GetChild(3).GetComponent<Button>();
            btn.onClick.AddListener( BtnClick );
        }

        private void OnChanged_InventoryState() {
            var inventoryClosed = InventoryController.Instance.IsClosed;
            if (!inventoryClosed) {
                while (Player.Objects.Active)
                    Player.Objects.TryStop();
            }
        }

        //ship list 

        private bool TryStart_ShipList() {
            if (!InventoryController.Instance.IsClosed)
                return false;
            m_Window.Open();
     
            var listObj = ShipsDictionary.Instance.shipDicKey;
            transform.GetComponent<ShowShipsList>().ShowAllShipsBtn( listObj );
            return true;
        }

        private bool TryStop_ShipList() {
            m_Window.Close();
            return true;
        }

        void BtnClick() {
            GameController.LocalPlayer.Objects.TryStop();
        }
    }
}
